<?php

class HmPaymentController extends Fw_Controller_Action
{
	public function init()
	{
		$this->_helper->layout->disableLayout();
		$this->initSession();
	}

	public function indexAction()
	{
		$offer_id = (int)$this->_request->getParam('offer_id');
		$model = $this->_getModel('HmPayment');
		$payment_list = $this->_model;
		$query = $payment_list->select()
							->setIntegrityCheck(false)
							->from($payment_list)
							->join('hm_currency', 'hm_currency.currency_id = hm_payment.currency_id')
							->where('offer_id='.$offer_id);
		$this->view->payment_list = $payment_list->fetchAll($query);
	}
	
	public function addAction()
	{
		$this->view->offer_id = (int)$this->_request->getParam('offer_id');
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('add');
            if ($formData) {
            	
				$model = $this->_getModel('HmPayment');
				$add   = $this->_model;
				
				$row              = $add->createRow();
                $row->value       = $this->_request->getPost('value');
                $row->date_limit  = $this->_request->getPost('date_limit');
                $row->offer_id    = (int)$this->_request->getPost('offer_id');
                $row->currency_id = $this->_request->getPost('currency_id');
				$row->save();
                
                return $this->_helper->redirector('index');
            }
        }
		require_once APPLICATION_PATH . '/models/HmCurrency.php';
		$currencyTable = new Model_HmCurrency();
		$currency_list = $currencyTable->fetchAll();
		$currencyRowCount = count($currency_list);
		
		$this->view->currency_list    = $currency_list;
		$this->view->currencyRowCount = $currencyRowCount;
	}
	
	public function editAction()
	{
		$model   = $this->_getModel('HmPayment');
		$edit    = $this->_model;
		$payment_id = (int)$this->_request->getParam('payment_id');
		
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('edit');
            if ($formData) {
				$payment_id = (int)$this->_request->getPost('payment_id');
                $row = $edit->fetchRow('payment_id='.$payment_id);
                $row->value       = $this->_request->getPost('value');
                $row->date_limit  = $this->_request->getPost('date_limit');
                $row->offer_id    = $this->_request->getPost('offer_id');
                $row->currency_id = $this->_request->getPost('currency_id');
                $row->save();
                
                return $this->_helper->redirector('index');
            }
        }
		$this->view->edit = $edit->fetchRow('payment_id='.$payment_id);
		
		require_once APPLICATION_PATH . '/models/HmCurrency.php';
		$currencyTable = new Model_HmCurrency();
		$currency_list = $currencyTable->fetchAll();
		$currencyRowCount = count($currency_list);
		
		$this->view->currency_list    = $currency_list;
		$this->view->currencyRowCount = $currencyRowCount;
	}
	
	public function showAction()
	{
		$payment_id = (int)$this->_request->getParam('payment_id');
				
		$model = $this->_getModel('HmPayment');
		$show = $this->_model;
		$this->view->show = $show->fetchRow('payment_id='.$payment_id);
	}
	
	public function deleteAction()
	{
		if ($this->_request->isPost()) {
            $payment_id = (int)$this->_request->getPost('payment_id');
            $del = $this->_request->getPost('del');
            if ($del == 'Yes' && $payment_id > 0) {
                
				$model = $this->_getModel('HmPayment');
				$remove = $this->_model;
				
                $where = 'payment_id = '.$payment_id;
                $remove->delete($where);
            }
            return $this->_helper->redirector('index');
        } else {
            $payment_id = (int)$this->_request->getParam('payment_id');
            if ($payment_id > 0) {
                
				$model = $this->_getModel('HmPayment');
				$remove = $this->_model;
				
                $this->view->remove = $remove->fetchRow('payment_id='.$payment_id);
            }
        }
	}
	/*
    protected function _getModel()
    {
        if (null === $this->_model) {
            require_once APPLICATION_PATH . 'models/HmPayment.php';
            $this->_model = new Model_HmPayment();
        }
        return $this->_model;
    }
	*/
}